package com.pe1n.yangtaopai.view.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.bean.LoginBean;

import java.util.Random;

import cn.bmob.v3.BmobObject;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class InjectionActivity extends AppCompatActivity implements View.OnClickListener {
    public static final int RESPOMSE_CODE = 100;
    private RelativeLayout back;
    private EditText editText_phone_injection;
    private EditText editText_yanzhengma_injection;
    private Button button_huoqu_injection;
    private EditText editText_name_injection;
    private EditText editText_psw_injection;
    private EditText editText_okpsw_injection;
    private Button button_OkZhuche_injection;
    private TextView textView_xieyi_injection;
    private int num;
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_injection);
        initView();

    }

    private void initView() {
        back = (RelativeLayout) findViewById(R.id.back);
        back.setOnClickListener(this);
        mContext = this;
        editText_phone_injection = (EditText) findViewById(R.id.edittext_phone_injection);
        editText_yanzhengma_injection = (EditText) findViewById(R.id.edittext_yanzhengma_injection);

        button_huoqu_injection = (Button) findViewById(R.id.button_huoqu_injection);
        button_huoqu_injection.setOnClickListener(this);

        editText_name_injection = (EditText) findViewById(R.id.edittext_name_injection);
        editText_psw_injection = (EditText) findViewById(R.id.edittext_psw_injection);
        editText_okpsw_injection = (EditText) findViewById(R.id.edittext_okpsw_injection);

        button_OkZhuche_injection = (Button) findViewById(R.id.button_zhuche_injection);
        button_OkZhuche_injection.setOnClickListener(this);

        textView_xieyi_injection = (TextView) findViewById(R.id.textview_xieyi_injection);
        textView_xieyi_injection.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.button_huoqu_injection:
                Random rnd = new Random();
                num = 1000 + rnd.nextInt(999);
                button_huoqu_injection.setText("重新获取");
                editText_yanzhengma_injection.setText(String.valueOf(num));
                break;
            case R.id.button_zhuche_injection:
                String yanzhengma = editText_yanzhengma_injection.getEditableText().toString();
                String phone = editText_phone_injection.getEditableText().toString();
                String name = editText_name_injection.getEditableText().toString();
                String psw = editText_psw_injection.getEditableText().toString();
                String okpsw = editText_okpsw_injection.getEditableText().toString();
                if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(name) || TextUtils.isEmpty(psw) || TextUtils.isEmpty(okpsw)) {
                    Toast.makeText(this, "信息不能为空", Toast.LENGTH_SHORT).show();
                } else if (editText_phone_injection.getText().toString().length() < 11) {
                    Toast.makeText(this, "手机号必须是11位", Toast.LENGTH_SHORT).show();
                } else if (psw.equals(okpsw) == false) {
                    Toast.makeText(this, "两次输入的密码不同", Toast.LENGTH_SHORT).show();
                } else if (editText_psw_injection.getText().toString().length() < 5) {
                    Toast.makeText(this, "密码不能小于5位", Toast.LENGTH_SHORT).show();
                } else if (yanzhengma.equals(String.valueOf(num)) == false) {
                    Toast.makeText(this, "验证码有误", Toast.LENGTH_SHORT).show();
                } else {
                    LoginBean mLoginBean=new LoginBean();
                    mLoginBean.setName(name);
                    mLoginBean.setPassword(psw);
                    mLoginBean.setPhone(phone);
                    mLoginBean.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                              if(e==null){
                               Toast.makeText(mContext,"注册成功!",Toast.LENGTH_LONG).show();
                                  SharedPreferences sharedPreferences=getSharedPreferences("myshared",0);
                                  sharedPreferences.edit().putBoolean("isRegister",true).commit();
                              }
                            else{
                                  Toast.makeText(mContext,"不好意思,注册失败!",Toast.LENGTH_LONG).show();
                              }
                        }
                    });


                    finish();
                }
                break;
            case R.id.textview_xieyi_injection:
                break;
        }

    }
}
